package com.dongdongsongcan.app.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.dongdongsongcan.app.R;
import com.su.dolphin.utils.mgr.DpActivityManager;
import com.su.dolphin.view.activity.BaseActivity;

public class WebViewActivity extends BaseActivity
{
    private WebView mWebView;
    // private ProgressDialog mProgressDialog;
    private Handler mHandler;
    
    public static void startFrom(Context context, Bundle bundle) {
        DpActivityManager.i().startActivity(context, WebViewActivity.class, bundle);
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        
    }
    
    @Override
    public void initData() {
        Bundle bundle = this.getIntent().getExtras();
        String urlString = bundle.getString("url");
        loadurl(mWebView, urlString);
    }
    
    @Override
    public void initView() {
        mHandler = new Handler()
        {
            public void handleMessage(Message msg) {// 定义一个Handler，用于处理下载线程与UI间通讯
                if (!Thread.currentThread().isInterrupted()) {
                    switch (msg.what) {
                        case 0:
                            setProgressBarIndeterminateVisibility(true);
                            break;
                        case 1:
                            setProgressBarIndeterminateVisibility(false);
                            break;
                    }
                }
                super.handleMessage(msg);
            }
        };
        mWebView = (WebView) findViewById(R.id.webView_wv);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        mWebView.setScrollBarStyle(0);
        mWebView.setWebViewClient(new WebViewClient()
        {
            public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
                loadurl(view, url);
                return true;
            }
            
        });
        mWebView.setWebChromeClient(new WebChromeClient()
        {
            public void onProgressChanged(WebView view, int progress) {
                if (progress == 100) {
                    mHandler.sendEmptyMessage(1);
                    setTitle("浏览器");
                }
                setTitle(progress + "%");
                super.onProgressChanged(view, progress);
            }
        });
    }
    
    public void loadurl(final WebView view, final String url) {
        mHandler.sendEmptyMessage(0);
        view.loadUrl(url);
    }
    
    public static void start(Context context) {
        Intent intent = new Intent(context, WebViewActivity.class);
        context.startActivity(intent);
    }
    
    @Override
    public void setListener() {
        
    }
    
    @Override
    public boolean onKeyDownFinish(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (mWebView.canGoBack()) {
                    mWebView.goBack();
                }
                else {
                    finish();
                }
                return true;
            default:
                break;
        }
        return super.onKeyDownFinish(keyCode, event);
    }
    // public void confirmExit() {// 退出确认
    // AlertDialog.Builder ad = new AlertDialog.Builder(WebViewActivity.this);
    // ad.setTitle("退出");
    // ad.setMessage("是否返回?");
    // ad.setPositiveButton("是", new DialogInterface.OnClickListener() {// 退出按钮
    // @Override
    // public void onClick(DialogInterface dialog, int i) {
    // WebViewActivity.this.finish();// 关闭activity
    //
    // }
    // });
    // ad.setNegativeButton("否", new DialogInterface.OnClickListener() {
    // @Override
    // public void onClick(DialogInterface dialog, int i) {
    // }
    // });
    // ad.show();
    // }
}
